1731E - Graph Cost - CodeForces Solution


dp greedy math number theory *2000

Please click on ads to support us..

Python Code:

import sys
readline=sys.stdin.readline
from collections import Counter

class Prime:
    def __init__(self,N):
        assert N<=10**8
        self.smallest_prime_factor=[None]*(N+1)
        for i in range(2,N+1,2):
            self.smallest_prime_factor[i]=2
        n=int(N**.5)+1
        for p in range(3,n,2):
            if self.smallest_prime_factor[p]==None:
                self.smallest_prime_factor[p]=p
                for i in range(p**2,N+1,2*p):
                    if self.smallest_prime_factor[i]==None:
                        self.smallest_prime_factor[i]=p
        for p in range(n,N+1):
            if self.smallest_prime_factor[p]==None:
                self.smallest_prime_factor[p]=p
        self.primes=[p for p in range(N+1) if p==self.smallest_prime_factor[p]]

    def Factorize(self,N):
        assert N>=1
        factors=defaultdict(int)
        if N<=len(self.smallest_prime_factor)-1:
            while N!=1:
                factors[self.smallest_prime_factor[N]]+=1
                N//=self.smallest_prime_factor[N]
        else:
            for p in self.primes:
                while N%p==0:
                    N//=p
                    factors[p]+=1
                if N<p*p:
                    if N!=1:
                        factors[N]+=1
                    break
                if N<=len(self.smallest_prime_factor)-1:
                    while N!=1:
                        factors[self.smallest_prime_factor[N]]+=1
                        N//=self.smallest_prime_factor[N]
                    break
            else:
                if N!=1:
                    factors[N]+=1
        return factors

    def Divisors(self,N):
        assert N>0
        divisors=[1]
        for p,e in self.Factorize(N).items():
            pow_p=[1]
            for _ in range(e):
                pow_p.append(pow_p[-1]*p)
            divisors=[i*j for i in divisors for j in pow_p]
        return divisors

    def Is_Prime(self,N):
        return N==self.smallest_prime_factor[N]

    def Totient(self,N):
        for p in self.Factorize(N).keys():
            N*=p-1
            N//=p
        return N

    def Mebius(self,N):
        fact=self.Factorize(N)
        for e in fact.values():
            if e>=2:
                return 0
        else:
            if len(fact)%2==0:
                return 1
            else:
                return -1

T=int(readline())
P=Prime(10**6)
for t in range(T):
    N,M=map(int,readline().split())
    cnt=[0]*(N+1)
    for g in range(1,N+1):
        cnt[g]=N//g*(N//g-1)//2
    for p in P.primes:
        if N<p:
            break
        for g in range(p,N+1,p):
            cnt[g//p]-=cnt[g]
    ans=0
    for g in range(N,1,-1):
        c=min(cnt[g],M)//(g-1)
        ans+=c*g
        M-=c*(g-1)
    if M:
        ans=-1
    print(ans)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
  int TC, n;
  ll m;
  cin >> TC;
  while (TC--){
    scanf("%d%lld", &n, &m);
    ll dp[n+1], res=0;
    for (int d = n; d >= 2; d--){
      dp[d] = ((n/d)*1ll*((n/d)-1))/2;
      for (int k = 2; k <= n/d; k++) dp[d] -= dp[k*d];
    }
    for (ll d = n; d >= 2; d--){
      ll t = min(m/(d-1), dp[d]/(d-1));
      m-=(d-1)*t, res+=d*t;
    }
    if(m) printf("-1\n");
    else printf("%lld\n", res);
  }   
}


Comments

Submit
0 Comments
More Questions

1358D - The Best Vacation
1620B - Triangles on a Rectangle
999C - Alphabetic Removals
1634C - OKEA
1368C - Even Picture
1505F - Math
1473A - Replacing Elements
959A - Mahmoud and Ehab and the even-odd game
78B - Easter Eggs
1455B - Jumps
1225C - p-binary
1525D - Armchairs
1257A - Two Rival Students
1415A - Prison Break
1271A - Suits
259B - Little Elephant and Magic Square
1389A - LCM Problem
778A - String Game
1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing